package com.timoon.webcomponent.utility.product.rank;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.Product;

/**
 *<p>排行,实际上是com.timoon.webcomponent.utility.product.discount.Discount1Panel拓展</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class RankPanel extends Panel {
	
	@SpringBean(name="com.timoon.sample.service.RankServiceImpl")
	private RankService service;
	
	private Class<? extends Page> targetPage;
	
	private String title;
	
	private Integer count;
	
	private PageParameters pageParameters=new PageParameters();
	
	public RankPanel(String id,String title,Class<? extends Page> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.title=title;
		
		this.targetPage=targetPage;
		
		renderPanel();
		
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		count=4;
		
		renderProduct1();
		
		
		renderTitle();
	}

	private void renderTitle() {
		// TODO Auto-generated method stub
		add(new Label("wk_title",title));
	}

	private void renderProduct1() {
		// TODO Auto-generated method stub
		List<Product> list=this.service.getRankProductList();
		
		List<Product> listWithPic=new ArrayList<Product>();
		List<Product> listWidthText=new ArrayList<Product>();
		
		if(list!=null&&list.size()>=3){
			listWithPic.add(list.get(0));
			listWithPic.add(list.get(1));
			listWithPic.add(list.get(2));
			
			int total=list.size();
			for(int i=3;i<total;i++){
				listWidthText.add(list.get(i));
			}
			
		}else{
			throw new RuntimeException("销售排行的product个数小于3");
		}
		
		
		ListView<Product> listView=new ListView<Product>("wk_loop",listWithPic) {
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				
				Product product=arg0.getModelObject();
		
				PageParameters parameters=new PageParameters();
				parameters.set("product_id", product.getId());
				
				BookmarkablePageLink link1=new BookmarkablePageLink<>("wk_link1",targetPage,parameters);
				arg0.add(link1);
				
				BookmarkablePageLink link2=new BookmarkablePageLink<>("wk_link2",targetPage,parameters);
				arg0.add(link2);
				
				link1.add(new Image("wk_img",new PackageResourceReference(this.getClass(),"name.png")));
				
				link2.add(new Label("wk_name",product.getName()));
				
				arg0.add(new Label("wk_instruction",product.getInstruction()));
				
			}
		};
		add(listView);
		
		ListView<Product> listText=new ListView<Product>("wk_loop_text",listWidthText) {
			
			
			@Override
			protected void populateItem(ListItem<Product> arg0) {
				// TODO Auto-generated method stub
				
				Product product=arg0.getModelObject();
				
				PageParameters parameters=new PageParameters();
				parameters.set("product_id", product.getId());
				
				BookmarkablePageLink link=new BookmarkablePageLink<>("wk_link_text", targetPage,parameters);
				arg0.add(link);
				
				
				arg0.add(new Label("wk_rank", "NO."+count));
				link.add(new Label("wk_text", product.getName()){{
					
				}});
				
				count++;
			}
		};
		add(listText);
		
		
	}

}
